/* 
 * File:   ParticleSystemManager.cpp
 * Author: RedEyedKiller
 * 
 * Created on 20 Αύγουστος 2010, 8:07 μμ
 */

#include "ParticleSystemManager.h"
#include "../parsers/ParticleSystemParser.h"
#include "../RenderingManager.h"
#include "../ResourceManager.h"
#include "../Globals.h"

namespace physicsSystem
{

namespace particleSystem
{

ParticleSystemManager::ParticleSystemManager()
{

}

ParticleSystemManager::~ParticleSystemManager()
{
    this->ClearParticleSystems();
}

void ParticleSystemManager::UpdateParticles(unsigned long deltaTime, LevelMap* map)
{
    std::map<std::string, ParticleSystem*>::iterator it = systems.begin();
    const std::map<std::string, ParticleSystem*>::iterator end = systems.end();
    ParticleSystem* curSystem;
    for (; it != end; ++it)
    {
        curSystem = it->second;
        //if system is not hidden or inactive
        if (curSystem->IsActive())
        {
            //add new particles to the system's emiters
            curSystem->AddParticles();
            //update particles
            curSystem->UpdateParticles(deltaTime);
            //and if collisions are enabled calculate them.
            if(curSystem->IsMapCollisions())
                curSystem->TileCollision(map);
        }
    }
}

Drawable* ParticleSystemManager::LoadParticleSystem(const std::string &name, bool active)
{
    ParticleSystem* system = new ParticleSystem;
    FileParser::ParticleSystemParser parser((Globals::GetInstance()->GetPath("Particles") + name + ".xml").c_str(), system);
    parser.ParseSystem();
    systems[name] = system;
    system->SetDisplay(ResourceManager::GetInstance()->GetTexture(system->GetResource()));
    system->SetActive(active);
    return system;
}

bool ParticleSystemManager::IsParticleSystemLoaded(const std::string &name)
{
    return (systems.find(name) != systems.end());
}

void ParticleSystemManager::DeleteParticleSystem(const std::string &name)
{
    ParticleSystem* system = systems[name];
    if (system != NULL)
    {
        delete system;
    }
}

void ParticleSystemManager::HideParticleSystem(const std::string &name)
{
    systems[name]->SetActive(false);
}

void ParticleSystemManager::ActivateParticleSystem(const std::string &name)
{
    systems[name]->SetActive(true);
}

ParticleSystem* ParticleSystemManager::GetParticleSystem(const std::string &name)
{
    return systems[name];
}

void ParticleSystemManager::ClearParticleSystems()
{
    SafeRelease<std::string,ParticleSystem*>(systems);
}

void ParticleSystemManager::SetParticleUpdateTime(int updateTime)
{
    this->particlesUpdateTime = updateTime;
}

int ParticleSystemManager::GetParticleUpdateTime() const
{
    return particlesUpdateTime;
}

}
}
